package com.haixiaoke.saas.enterprise.house.controller;

import com.haixiaoke.saas.common.annotation.RepeatSubmit;
import com.haixiaoke.saas.common.annotation.TenantScope;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.tenant.TenantContext;
import com.haixiaoke.saas.common.utils.poi.ExcelUtil;
import com.haixiaoke.saas.house.domain.Unit;
import com.haixiaoke.saas.house.service.IUnitService;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 单元Controller
 *
 * @author xiaolong
 * @date 2023-10-17
 */
@RestController
@RequestMapping("/enterprise/house/unit")
public class EnterpriseUnitController extends BaseController {
    @Autowired
    private IUnitService unitService;

    /**
     * 查询单元列表
     */
    @GetMapping("/list")
    @TenantScope
    public TableDataInfo list(Unit unit) {
        startPage();
        unit.setTenantId(TenantContext.getTenantId());
        List<Unit> list = unitService.selectUnitList(unit);
        return getDataTable(list);
    }

    /**
     * 导出单元列表
     */
    @PostMapping("/export")
    @TenantScope
    public void export(HttpServletResponse response, Unit unit) {
        unit.setTenantId(TenantContext.getTenantId());
        List<Unit> list = unitService.selectUnitList(unit);
        ExcelUtil<Unit> util = new ExcelUtil<Unit>(Unit.class);
        util.exportExcel(response, list, "单元数据");
    }

    /**
     * 获取单元详细信息
     */
    @GetMapping(value = "/{unitId}")
    public AjaxResult getInfo(@PathVariable("unitId") String unitId) {
        return success(unitService.selectUnitByUnitId(unitId));
    }

    /**
     * 新增单元
     */
    @PostMapping
    @TenantScope
    @RepeatSubmit
    public AjaxResult add(@RequestBody Unit unit) {
        unit.setTenantId(TenantContext.getTenantId());
        return toAjax(unitService.insertUnit(unit));
    }

    /**
     * 修改单元
     */
    @PutMapping
    @TenantScope
    @RepeatSubmit
    public AjaxResult edit(@RequestBody Unit unit) {
        return toAjax(unitService.updateUnit(unit));
    }

    /**
     * 删除单元
     */
    @DeleteMapping("/{unitIds}")
    @TenantScope
    @RepeatSubmit
    public AjaxResult remove(@PathVariable String[] unitIds) {
        return toAjax(unitService.deleteUnitByUnitIds(unitIds));
    }
}
